package com.loyin.task;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.loyin.model.DownloadUrl;
import com.loyin.model.WebSite;
import com.loyin.tool.AnalysisUrl;

/**
 * 任务调度
 * @author 刘声凤
 * 2011-11-13
 */
public class WebTask{
	private Logger log=Logger.getLogger(this.getClass());
	public void work(){
		final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int pagesize=100;
		int pagestart=1;
		List<Thread> threadlist=new ArrayList<Thread>(10);
		try{
			/**第一步：获得监控网址*/
			StringBuffer sql=new StringBuffer(" 1=1");
			int i=1;
			List<WebSite> websitelist= WebSite.findAll(WebSite.class, sql.toString(),null," addtime desc ",pagesize,pagestart);
				do{
					/**第二步：对监控网址分段进行分析*/
					if(websitelist!=null&&websitelist.isEmpty()==false){
						final List<WebSite> weblist=websitelist;
						Thread thread=new Thread(){
							public void run(){
								for(WebSite website:weblist){
									String recordtime=sdf.format(new Date());
									long userid=website.userid;
									AnalysisUrl aurl= new AnalysisUrl();
									try{
										List<Map<String,Object>>list=aurl.analysis(website.url, website.keyword);
										int count=0;
										if(list!=null&&list.isEmpty()==false){
											for(Map<String,Object> mp:list){
												DownloadUrl durl=new DownloadUrl(userid, website.id, mp.get("title").toString() , mp.get("href").toString(),Integer.parseInt(mp.get("count").toString()),recordtime);
												try{
													durl.save();
													count++;
												}catch(Exception e){continue;}
											}
										}
									}catch(Exception e){
										log.error("",e);
										continue;
									}
								}
							}
						};
						thread.start();
						threadlist.add(thread);
					}
					i++;
					pagestart=pagesize*i;
					websitelist= WebSite.findAll(WebSite.class, sql.toString(),null," addtime desc ",pagesize,pagestart);
				}while(websitelist!=null&&websitelist.isEmpty()==false);
				
				/**第三步：获得有需要下载网页的会员列表，及生成对应的wget url文件，再调wget命令下载*/
				
		}catch(Exception e){
			log.error("",e);
		}
	}
}
